/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.smp.controler;

import br.gov.cemaden.library.model.smp.CPU;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

/**
 *
 * @author Simone Santos
 */
public class CPUInfoBean implements Serializable {

    CPU cpu = new CPU();

    public CPUInfoBean() {
    }

    public CPU getCPU() {
        Sigar sigar = new Sigar();
        try {
            CpuPerc cpuPerc = sigar.getCpuPerc();
            double cpuUsageFormatada = Math.round((cpuPerc.getCombined() * 100) * 100.0) / 100.0;
            cpu.setCpuUsage(cpuUsageFormatada);

        } catch (SigarException ex) {
            Logger.getLogger(CPUInfoBean.class.getName()).log(Level.SEVERE, null, ex);
        }


        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

        try {
            long uptime = sigar.getCpu().getTotal();
            String d = uptime / (3600 * 1000 * 24) + ":" + dateFormat.format(uptime);
            cpu.setTotalSystemTime(d);

            CpuInfo[] cpuInfoList = sigar.getCpuInfoList();
            for (CpuInfo cpuInfo : cpuInfoList) {
                double cache = (double) (cpuInfo.getCacheSize());

                cpu.setCacheSize(cache);
                cpu.setCorPerSockets(cpuInfo.getCoresPerSocket());
                cpu.setModel(cpuInfo.getModel());
                cpu.setTotalCores(cpuInfo.getTotalCores());
                cpu.setTotalSockets(cpuInfo.getTotalSockets());
                cpu.setVendor(cpuInfo.getVendor());
            }
        } catch (SigarException e) {
            System.out.println("Erro no CPUInfoBean.setAllInformationCPU: " + e.getMessage() + " - " + e.getCause());
        }
        return cpu;
    }

    public void show(CPU t) {
        System.out.println("CpuUsage: " + t.getCpuUsage());
        System.out.println("TotalSystemTime: " + t.getTotalSystemTime());
        System.out.println("CacheSize: " + t.getCacheSize());
        System.out.println("CorPerSockets: " + t.getCorPerSockets());
        System.out.println("Model: " + t.getModel());
        System.out.println("TotalCores: " + t.getTotalCores());
        System.out.println("TotalSockets: " + t.getTotalSockets());
        System.out.println("Vendor: " + t.getVendor());
        System.out.println("--------------------------------\n");
    }
}
